package cn.com.homeyoda.aimate.aihealth.ui.bluetooth;

import android.app.Application;
import android.os.Build;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.bean.BaseResponse2;
import cn.com.homeyoda.aimate.aihealth.bean.FeedbackTypeBean;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.FEEDBACK_SUCCESS;

/**
 * Created By liuxinhua
 * Created Time 2025/2/27
 */
public class BluetoothSuccessModel extends BaseViewModel<Repository> {

    public StateLiveData<List<FeedbackTypeBean.DataBean>> feedbackType;

    public BluetoothSuccessModel(@NonNull Application application, Repository model) {
        super(application, model);
        feedbackType = new StateLiveData<>();
        feedbackType.setValue(new ArrayList<>());
    }

    public void getFeedBackType() {
        model.getFeedBackType()
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(BluetoothSuccessModel.this)
                .subscribe(new Observer<FeedbackTypeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        feedbackType.postLoading();
                    }

                    @Override
                    public void onNext(FeedbackTypeBean response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            feedbackType.postValueAndSuccess(response.getData());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        feedbackType.postError();
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    public void uploadFeedBack(String type, String describe, String phone) {
        model.uploadFeedBack(getJsonObject(type, describe, phone))
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(BluetoothSuccessModel.this)
                .subscribe(new Observer<BaseResponse2>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse2 response) {
                        if (response.getCode() == 200) {
                            KLog.d(TAG, "onNext response = " + response);
                            ToastUtils.showShort("反馈成功");
                            LiveEventBus.get(FEEDBACK_SUCCESS).post(true);
                        } else {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }


                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                    }
                });
    }

    private @NonNull JsonObject getJsonObject(String type, String describe, String phone) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("userId", model.getUserId());
        jsonObject.addProperty("type", type);
        jsonObject.addProperty("describe", describe);
        jsonObject.addProperty("name", model.getUserName());
        jsonObject.addProperty("tel", phone);
        jsonObject.addProperty("channel", "2000");
        jsonObject.addProperty("deviceType", "");
        jsonObject.addProperty("deviceSn", Build.SERIAL);
        return jsonObject;
    }


}
